﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    public class FlyTextManager: BattleSingleton<FlyTextManager>
    {
        private class FlyText : IClass
        {
            public BattleFlyTextObject gameObject;
            public float startTime;
            public float duration;
            public void OnReset()
            {
                
            }
        }
        
        private List<FlyText> flyTextList = new List<FlyText>();
        private GameObject rootObject;
        public Transform Root => rootObject.transform;
        private Canvas canvas;
        public override void Initialize()
        {
            rootObject = new GameObject("FlyTextRoot");
            Root.transform.parent = SurvivorManager.Instance.Root;
            var canvas = rootObject.AddComponent<Canvas>();
            canvas.renderMode = RenderMode.WorldSpace;
            canvas.worldCamera = CameraManager.Instance.HudCamera;
            this.canvas = canvas;
        }

        public void SetCamera(Camera camera)
        {
            canvas.worldCamera = camera;
        }
        
        public override void Update()
        {
            for (int i = 0; i < flyTextList.Count; i++)
            {
                var text = flyTextList[i];
                if (Time.time - text.startTime > text.duration)
                {
                    BattlePoolManager.Instance.ReleaseFlyText(text.gameObject);
                    ClassManager.Free(text);
                    if (i < flyTextList.Count - 1)
                    {
                        flyTextList[i] = flyTextList[flyTextList.Count - 1];
                    }
                    flyTextList.RemoveAt(i);
                    i--;
                }
            }
        }

        public void ShowFlyText(EFlyTextType type, Vector3 position, string text, float duration = 1)
        {
            var flyText = ClassManager.Get<FlyText>();
            flyText.startTime = Time.time;
            flyText.duration = duration;
            flyText.gameObject = BattlePoolManager.Instance.GetFlyText((int)type);
            flyText.gameObject.Position = position;
            flyText.gameObject.SetText(text);
            flyText.gameObject.Forward = CameraManager.Instance.CameraForward;
            flyTextList.Add(flyText);
        }
        public void ShowFlyTextByResID(int resID, Vector3 position, string text, float duration = 1)
        {
            var flyText = ClassManager.Get<FlyText>();
            flyText.startTime = Time.time;
            flyText.duration = duration;
            flyText.gameObject = BattlePoolManager.Instance.GetFlyText(resID);
            flyText.gameObject.Position = position;
            flyText.gameObject.SetText(text);
            flyText.gameObject.Forward = CameraManager.Instance.CameraForward;
            flyTextList.Add(flyText);
        }

        public override void Dispose()
        {
            for (int i = 0; i < flyTextList.Count; i++)
            {
                var text = flyTextList[i];
                BattlePoolManager.Instance.ReleaseFlyText(text.gameObject);
                ClassManager.Free(text);
            }
            flyTextList.Clear();
            GameObject.DestroyImmediate(rootObject);
        }
    }
}
